package com.situ.mall.order.controller;

import com.situ.mall.common.exception.BusinessException;
import com.situ.mall.common.service.IMemberService;
import com.situ.mall.order.util.SendMsgUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@Controller
@RequestMapping("/")
public class RegisterController {

    @Autowired
    private IMemberService memberService;
    @Resource
    private RedisTemplate<String ,Object> redisTemplate;
@GetMapping("/register")
    public String register() {

    return "register";
    }

    @GetMapping("/msgCode")
    @ResponseBody
    public void sendMsg(String tel) throws Exception {
        //判断此手机号是否注册过

        String code="2233333";
        redisTemplate.opsForValue().set("reg-code-"+tel,code,5, TimeUnit.MINUTES);
        SendMsgUtil.sendMsg(tel,code);
    }

    @PostMapping("/reg")
    @ResponseBody
    public void  reg(String tel,String code)throws Exception{
    Object obj =redisTemplate.opsForValue().get("reg-code-"+tel);
        //判断code是否正确
        if(obj==null || !((String)obj).equals(code)){
            throw new BusinessException("短信验证码输入不正确");
        }
        SendMsgUtil.sendMsg(tel, "332222");//只存5分钟，使用redis
        //如果正确，插入一条member会员数据


    }
}
